<?php

namespace app\shopapi\controller;

use app\admin\model\shop\Category;
use app\api\model\Pay;
use app\common\controller\Shopapi;
use app\shopapi\model\pay\ShopInfeed;
use app\shopapi\model\ShopAdmin;
use app\shopapi\model\ShopBillsStat;
use app\shopapi\model\ShopUser;
use think\Log;

/**
 * 店铺接口
 */
class Shop extends Shopapi
{
    protected $noNeedLogin = ['category','scanback','ceshiaddlog','ceshiadd','idRecognition'];
    protected $noNeedRight = '*';

    //添加申请记录
    public function addlog()
    {
        $model = new \app\shopapi\model\Shop();
        $shop_no = \fast\Random::numeric(10);
        $data = [
            'user_id'=> $this->shopauth->id,
            'shop_no'=>$shop_no
        ];
        $res = $model->save($data);
        if ($res){
            $admin_model = new ShopAdmin();
            $admin_data = [
                'shop_id' => $model->shop_id,
                'is_applicant'=>1,
                'realname'=>$this->shopauth->nickname,
                'mobile'=>$this->shopauth->mobile,
                'shop_no'=>$shop_no,
                'collection_code'=>$shop_no.'-'.\fast\Random::numeric(6)
            ];
            $admin_model->save($admin_data);
        }

        $this->success('',$model->shop_id);
    }

    //删除店铺
    public function del()
    {
        $shop_id = input('shop_id',0);
        $shop = \app\shopapi\model\Shop::get($shop_id);
        if (!$shop)$this->error('该记录不存在');
        $res = $shop->delete();
        if ($res){
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    //申请入驻
    public function create()
    {
        $params = input();
        try {

            $model = new \app\shopapi\model\Shop();
            $params['user_id']=$this->shopauth->id;
            $res = $model->add($params);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);

    }

    //保存草稿
    public function draft()
    {
        $params = input();
        try {

            $model = new \app\shopapi\model\Shop();
            $params['user_id']=$this->shopauth->id;
            $res = $model->draft($params);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);

    }

    //店铺列表
    public function lists()
    {
        try {

            $model = new \app\shopapi\model\Shop();

            $res = $model->lists($this->shopauth->id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //申请的店铺信息
    public function detail()
    {
        $shop_id = input('shop_id',0);
        $shop = \app\shopapi\model\Shop::get($shop_id);
        $this->success('',$shop);
    }

    //查询地推手机号
    public function searchUser()
    {
        $mobile = input('mobile','');
        $user = \app\common\model\User::where(['mobile'=>$mobile])
            ->where('level','>',1)
            ->field('id,avatar,nickname,mobile')->find();
        if (!$user){
            $this->error('用户不存在');
        } else {
            $this->success('',$user);
        }
    }

    //绑定地推
    public function bindRecommender()
    {
        $mobile = input('mobile','');
        if (empty($mobile))$this->error('请输入手机号');
        $user = \app\common\model\User::where(['mobile'=>$mobile,'level'=>2])->field('id,avatar,nickname,mobile')->find();
        if (!$user){
            $this->error('用户不存在');
        } else {
            $shop_user = ShopUser::get($this->shopauth->id);
            $shop_user->recommender_id = $user['id'];
            $shop_user->save();
            $this->success('绑定成功');
        }
    }

    //是否已绑定地推
    public function isBindRecommender()
    {
        $this->success('',$this->shopauth->recommender_id);
    }

    //经营行业
    public function category()
    {
        $lists = Category::where(['status'=>'normal','pid'=>0])->order('weigh desc')->field('id,name')->select();

        foreach ($lists as $k=>$v){
            $lists[$k]['child'] = Category::where(['status'=>'normal','pid'=>$v['id']])->order('weigh desc')->field('id,name')->select();
        }

        $this->success('',$lists);
    }


    //店铺资料
    public function shopInfo()
    {
        $model = new \app\shopapi\model\Shop();

        $data = $model->shopInfo($this->shopauth->id);

        $this->success('',$data);
    }

    //修改店铺佣金比例
    public function editCommission()
    {
        $commission_rate = input('commission_rate',0);
        $shop = \app\shopapi\model\Shop::get($this->shopauth->shop_id);

        $shop->commission_rate = $commission_rate;
        $res = $shop->save();
        $this->success('',$res);
    }

    //修改店铺基础信息
    public function editShopBasisInfo()
    {
        $params = input();
        try {
            $model = new \app\shopapi\model\Shop();
            $params['shop_id'] = $this->shopauth->shop_id;
            $res = $model->editShopBasisInfo($params);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //店铺基础信息
    public function shopBasisInfo()
    {
        $shop = \app\shopapi\model\Shop::get($this->shopauth->shop_id);

        $data = [
            'shop_name'=>$shop['shop_name'],
            'category_id'=>$shop['category_id'],
            'address'=>$shop['address'],
            'lat'=>$shop['lat'],
            'lng'=>$shop['lng'],
            'start_time'=>$shop['start_time'],
            'end_time'=>$shop['end_time'],
            'work_week'=>$shop['work_week'],
            'banner_img'=>$shop['banner_img'],
            'interior_img'=>$shop['interior_img'],
            'group_photo_img'=>$shop['group_photo_img'],
            'realname'=>$shop['realname'],
            'mobile'=>$shop['mobile'],
            'email'=>$shop['email'],
        ];

        $this->success('',$data);
    }

    //修改店铺资质信息
    public function editShopAptitudeInfo()
    {
        $params = input();
        try {
            $model = new \app\shopapi\model\Shop();
            $params['shop_id'] = $this->shopauth->shop_id;
            $res = $model->editShopAptitudeInfo($params);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //店铺资质信息
    public function shopAptitudeInfo()
    {
        $shop = \app\shopapi\model\Shop::get($this->shopauth->shop_id);

        $data = [
            'front_id_card_img'=>$shop['front_id_card_img'],
            'reverse_id_card_img'=>$shop['reverse_id_card_img'],
            'id_card_name'=>$shop['id_card_name'],
            'id_card_num'=>$shop['id_card_num'],
            'id_card_start_time'=>$shop['id_card_start_time'],
            'id_card_end_time'=>$shop['id_card_end_time'],
            'legal_person_phone'=>$shop['legal_person_phone'],
            'signature_img'=>$shop['signature_img'],
        ];
        $this->success('',$data);
    }


    //商户入驻回调
    /*public function back()
    {
        $param = input();
        Log::error('商户入驻回调:'.json_encode($param));
        $model = new ShopInfeed();
        $res = $model->notify_url($param);
        $this->success('',$res);
    }*/

    //聚合码
    public function qrcode()
    {
        try {

            $model = new \app\shopapi\model\lakala\ShopInfeed();

            $res = $model->qrcode($this->shopauth->shop_id,$this->shopauth->id);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //设置收款码
    public function setQrcode()
    {
        try {
            $remark = input('remark','');

            $shop_admin_id = input('shop_admin_id','');

            $model = new \app\shopapi\model\lakala\ShopInfeed();

            $res = $model->setQrcode($this->shopauth->shop_id,$this->shopauth->id,$remark,$shop_admin_id);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //微信扫一扫回调接口
   /* public function scanback()
    {
        $data = input();

        Log::error('微信扫一扫回调'.json_encode($data,320));
        try {
            $pay_model = new ShopInfeed();
            $res = $pay_model->scanback($data);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }*/

    //修改达人查看数据权限
    public function editViewData()
    {
        $view_data = input('view_data',1);
        $shop = \app\shopapi\model\Shop::get($this->shopauth->shop_id);
        $shop->view_data = $view_data;
        $shop->save();
        $this->success('');
    }

    //达人查看数据 回显
    public function viewData()
    {
        $shop = \app\shopapi\model\Shop::get($this->shopauth->shop_id);

        $this->success('',$shop->view_data);
    }

    public function ceshiadd()
    {
        $params = input();
        try {

            $model = new \app\shopapi\model\Shop();
            $params['user_id']=$this->shopauth->id;
            $res = $model->ceshiadd($params);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //添加申请记录
    public function ceshiaddlog()
    {
        $user_id = input('user_id',0);
        if (empty($user_id))$this->error('缺少必传参数user_id');
        $model = new \app\shopapi\model\Shop();
        $shop_no = \fast\Random::numeric(10);
        $data = [
            'user_id'=>$user_id,
            'shop_no'=>$shop_no
        ];
        $shop_user = ShopUser::get($user_id);
        $res = $model->save($data);
        if ($res){
            $admin_model = new ShopAdmin();
            $admin_data = [
                'shop_id' => $model->shop_id,
                'is_applicant'=>1,
                'realname'=>$shop_user->nickname,
                'mobile'=>$shop_user->mobile,
                'shop_no'=>$shop_no,
                'collection_code'=>$shop_no.'-'.\fast\Random::numeric(6)
            ];
            $admin_model->save($admin_data);
        }

        $this->success('',$model->shop_id);
    }

    //证件识别
    public function idRecognition()
    {
        $base64 = input('base64','');

        $cardType = input('cardType',0);

        $apiUrl = 'http://v.juhe.cn/certificates/queryBase64';

        $params = [
            'key' => 'b8ae3cd28a03beaa04ba9a24ff7a554f', // 您申请到的接口请求key
            'cardType' => $cardType, // 证件类型，依据支持的证件类型清单id修改
            'base64' => $base64
        ];

        // 发起网络请求证件识别接口
        $response = juheHttpRequest($apiUrl, $params, 1);
        $result = json_decode($response, true);
        if ($result['error_code']==0){
            $this->success('',$result['result']);
        } else {
            $this->error($result['reason']);
        }

    }
}